<?php

namespace App\Service;
use Hyperf\DbConnection\Db;

class SystemService
{
    public function user_message(){
        $where = [
            ['status', '=', '1'],
            ['deleted', '=', '0'],
        ];
        $message_list = Db::table('base_user_message')->where($where)->orderBy('sort',"desc")->select('id','name',"jump_id","jump_type")->get();
        if (empty($message_list)){
            $message_list = (object)[];
        }
        return $message_list;
    }

    public function banner_list(){
        $where = [
            ['status', '=', '1'],
            ['deleted', '=', '0'],
        ];
        $message_list = Db::table('base_banner')->where($where)->orderBy('sort',"desc")->select('id','name',"cover","jump_id","jump_type")->get();
        if (empty($message_list)){
            $message_list = (object)[];
        }
        return $message_list;
    }

    public function popup_goods_info(){
        # 弹出商品信息
        $where = [
            ['status', '=', '1'],
            ['deleted', '=', '0'],
        ];
        $goods_list = Db::table('base_home_recomm')->where($where)->orderBy('sort',"desc")->select('id','name',"cover","jump_id","jump_type")->forPage(1,5)->get();
        $goods_list = self::object_to_array($goods_list);
        if (empty($goods_list)){
            $goods_info =(object)[];
        }else{
            if (count($goods_list)>1){
                $number = count($goods_list) -1;
                $star_rud = mt_rand(0,$number);
            }else{
                $star_rud = 0;
            }
            $goods_info = $goods_list[$star_rud];
        }
        return $goods_info;
    }

    public function object_to_array($obj){
       # 对象列表装好为空
       $new_array = [];
       foreach ($obj as $key=>$value){
           $new_array[$key] = (array)$value;
       }
       return $new_array;
    }

    public function common_problem($s_type){
        $where = [
            "s_type"=>$s_type,
            "status"=>1,
            "deleted"=>0,
        ];
        $info = Db::table('data_news_item')->where($where)->select('id',"name","content")->first();
        if (count((array)$info)>0){
        }else{
            $info = (object)$info;
        }
        return $info;
    }
}